AVCastManager

Kind of class:class
Inherits from:none
Version:1.0.0
Author:The gotoAndPlay() Team
http://www.smartfoxserver.com
http://www.gotoandplay.it
Classpath:com.smartfoxserver.redbox.AVCastManager
File last modified:Monday, 07 July 2008, 10:21:32
SmartFoxServer's RedBox Audio/Video Broadcast Manager.
This class is responsible for managing audio/video live casts inside the room joined by the user, making it possible to create live web events or a/v conferences.
The AVCastManager handles the live cast publishing/playing to/from the Red5 server.
Unlike the other RedBox classes, the AVCastManager works on a room basis to leverage the access control and moderation features of SmartFoxServer rooms.

NOTE: in the provided examples, avCastMan always indicates an AVCastManager instance.
Usage:
  • The most common usages for the AVCastManager class are video conference applications and live webcast applications (for example live online seminars).

    Video conference
    In this kind of application, each user publishes his own live stream and subscribes the streams coming from the other users in the same room (in the SmartFoxServer meaning). The following workflow is suggested.
    1. The current user joins the room where the video conference takes place.
    2. The list of currently available streams (or "live casts") is retrieved by means of the getAvailableCasts method (in case the conference is already in progress).
      Calling this method also enables the reception of the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastPublished and com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastUnpublished events which notify that another user started or stopped his own stream.
    3. Each live cast is subscribed by means of the subscribeLiveCast method and a Video object is displayed on the stage to attach the stream to.
      When a live cast is published (a new user joins the conference) or stopped (a user leaves the conference), a notification is received by means of the above mentioned events: the stream is subscribed / unsubscribed and displayed on / removed from the stage.
    4. The a/v stream for the current user is published by means of the publishLiveCast method and an additional Video object showing the user own camera stream is added on the stage; when the current user publishes his own stream, the other users receive the above mentioned events.
    5. To make the current user leave the conference, the unpublishLiveCast method is called. Also changing SmartFoxServer room causes the user to leave the conference and the other users to be notified.

    Live webcast
    In this kind of application, a single user publishes his own live stream and all the other users in the same room subscribe it. The following workflow is suggested.
    1. The publisher joins the room where the live webcast takes place.
    2. The a/v stream for the user is published by means of the publishLiveCast method and a Video object showing the user own camera stream is added on the stage; when the current user publishes his own stream, the other users already in the room receive the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastPublished event and the stream can be subscribed (see step 4).
    3. The webcast spectators join the room and retrieve the list of available streams (one item only if the publisher is already streaming, otherwise the list will be empty).
    4. If the live webcast is already started, it can be subscribed by means of the subscribeLiveCast method; otherwise the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastPublished event must be waited for before calling this method.
    5. On all the subscribers clients a Video object is displayed on the stage to attach the stream to.
Events broadcasted to listeners:
  • RedBoxCastEvent with type: onAVConnectionInited
    • Dispatched when the connection to Red5 server has been established.
  • RedBoxCastEvent with type: onAVConnectionError
    • Dispatched when the connection to Red5 server can't be established.
  • RedBoxCastEvent with type: onLiveCastPublished
    • Dispatched when a user in the current room published his own live stream.
  • RedBoxCastEvent with type: onLiveCastUnpublished
    • Dispatched when a user in the current room stops his own live stream.

Summary


Constructor
  • AVCastManager (red5Ip:String, debug:Boolean)
    • AVCastManager contructor.
Instance properties
  • isConnected : Boolean
    • The status of the connection to the Red5 server.
Instance methods
  • initAVConnection : Void
    • Initialize the audio/video connection.
  • destroy : Void
    • Destroy the AVCastManager instance.
  • getAvailableCasts : Array
    • Retrieve the list of available live broadcasts for the current room.
  • stopPublishNotification : Void
    • Stop receiving live cast published/unpublished events.
  • subscribeLiveCast (castId:String) : NetStream
    • Subscribe a live cast to receive its audio/video stream.
  • unsubscribeLiveCast (castId:String) : Void
    • Unsubscribe a live cast to stop receiving its audio/video stream.
  • unsubscribeAllLiveCasts : Void
    • Unsubscribe all currently subscribed live casts.
  • publishLiveCast (enableCamera:Boolean, enableMicrophone:Boolean) : NetStream
    • Start broadcasting the current user's live stream.
  • unpublishLiveCast : Void
    • Stop broadcasting the current user's live stream.
  • addEventListener (type:String, listener:Function) : Void
    • Register a listener function in order to receive notification of a RedBox event.
  • removeEventListener (type:String, listener:Function) : Void
    • Remove a listener from the event dispatcher, to stop receiving notification of an event.

Constructor

AVCastManager

function AVCastManager (
red5Ip:String, debug:Boolean)

AVCastManager contructor.
The RedBox classes make an extensive use of the SmartFoxBits Connector's s event handler in order to communicate with SmartFoxServer.
To achieve this you should place the Connector on a frame before you use RedBox classes on the following frame(s) or to make sure that the Connector is on the lowest layer of the timeline, and set the "Load order" option in the Flash Publish Settings to "Bottom up".
Parameters:
sfs :
the SmartFoxClient instance.
red5Ip:
the Red5 server IP address.
debug :
turn on the debug messages (optional, default is false).
Throws:
Example:
  • The following example shows how to instantiate the AVCastManager class.
    var red5IpAddress:String = "127.0.0.1"
    
    var avCastMan:AVCastManager = new AVCastManager(red5IpAddress)

    see MyUserPropsNotSetException

Instance properties

isConnected

isConnected:Boolean
(read)

The status of the connection to the Red5 server.
If true, the connection to Red5 is currently available.

Instance methods

addEventListener

function addEventListener (
type:String, listener:Function) : Void

Register a listener function in order to receive notification of a RedBox event.

If you no longer need an event listener, remove it by calling the removeEventListener method.

NOTE: use the Delegate.create() method when registering a listener, to keep right scope in your application.
Parameters:
type :
The event type you want to be notified of.
listener:
The listener function in your application that processes the event. This function must accept an RedBoxCastEvent object as its only parameter and must return nothing, as in the example above.
Usage:
  • To register to an event:
    import com.smartfoxserver.redbox.events.*
    import mx.utils.Delegate
    
    avCastMan.addEventListener(RedBoxCastEvent.onLiveCastPublished, Delegate.create(this, myListener))
    
    private function myListener(evt:RedBoxCastEvent):Void
    {
        trace("Got the event!")
        trace("Event type: " + evt.type)
        trace("Event target: " + evt.target)
        trace("Additional parameters:")
    
        for (var i in evt.params)
            trace("\t" + i + " -> " + evt.params[i])
    }

destroy

function destroy (
) : Void

Destroy the AVCastManager instance.
Calling this method causes the interruption of all the playing streams (if any) and the disconnection from Red5.
This method should always be called before deleting the AVCastManager instance.
Example:
  • The following example shows how to destroy the AVCastManager instance.
    avCastMan.destroy()
    avCastMan = null

getAvailableCasts

function getAvailableCasts (
) : Array

Retrieve the list of available live broadcasts for the current room.
The list is populated by the AVCastManager class as soon as it is instantiated and each time a new room is joined.
When this method is called, the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastPublished and com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastUnpublished events dispatching is enabled, in order to be notified when users in the current room start/stop streaming.
In order to turn off events notification, the stopPublishNotification method should be called.
Returns:
Example:
  • The following example shows how to loop through the list of live casts available for the current room.
    var liveCasts:Array = avCastMan.getAvailableCasts()
       for(var i in avCastMan.getAvailableCasts())
       {
        var liveCast:LiveCast = liveCasts[i]
           // Subscribe live cast
           var stream:NetStream = avCastMan.subscribeLiveCast(liveCast.id)
    
           // Display a/v stream on stage
           ...
       }

initAVConnection

function initAVConnection (
) : Void

Initialize the audio/video connection.
Calling this method causes the connection to Red5 to be established and the com.smartfoxserver.redbox.events.RedBoxCastEvent.onAVConnectionInited event to be fired in response.
If the connection can't be established, the com.smartfoxserver.redbox.events.RedBoxCastEvent.onAVConnectionError event is fired in response.
NOTE: this method is called automatically when the AVCastManager is instantiated.
Throws:
Events broadcasted to listeners:
  • RedBoxCastEvent with type: onAVConnectionInited
    • Dispatched when the connection to Red5 server has been established.
  • RedBoxCastEvent with type: onAVConnectionError
    • Dispatched when the connection to Red5 server can't be established.
Example:
  • The following example shows how to initialize the Red5 connection for the AVCastManager instance.
    avCastMan.initAVConnection()

publishLiveCast

function publishLiveCast (
enableCamera:Boolean, enableMicrophone:Boolean) : NetStream

Start broadcasting the current user's live stream.
Calling this method causes the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastPublished event to be fired on the other users clients.
Audio and video recording mode/quality should be set before calling this method. In order to alter these settings, please refer to the Microphone and Camera classes documentation.
Parameters:
enableCamera :
enable video live streaming; default value is true.
enableMicrophone:
enable audio live streaming; default value is true.
Returns:
  • The NetStream object representing the user's outgoing stream.
Events broadcasted to listeners:
  • RedBoxCastEvent with type: onLiveCastPublished
    • Dispatched when a user in the current room published his own live stream.
Throws:
Example:
  • The following example shows how to publish the current user's live stream.
    avCastman.publishLiveCast(true, true)

removeEventListener

function removeEventListener (
type:String, listener:Function) : Void

Remove a listener from the event dispatcher, to stop receiving notification of an event.
Parameters:
type :
The event type you registered your listener to.
listener:
The listener function to remove.
Usage:
  • To remove a listener:
    avCastMan.removeEventListener(RedBoxCastEvent.onLiveCastPublished, myListener)
    NOTE: the Delegate.create() is not required to remove a listener.

stopPublishNotification

function stopPublishNotification (
) : Void

Stop receiving live cast published/unpublished events.

subscribeLiveCast

function subscribeLiveCast (
castId:String) : NetStream

Subscribe a live cast to receive its audio/video stream.
Parameters:
castId:
(String) the id of the com.smartfoxserver.redbox.data.LiveCast object to be subscribed.
Returns:
  • A NetStream object.
Throws:
Example:
  • The following example shows how to subscribe a live cast when a publishing notification is received.
    avCastMan.addEventListener(RedBoxCastEvent.onLiveCastPublished, Delegate.create(this, onLiveCastPublished))
    
    // A user publishes his own live cast...
    
    function onLiveCastPublished(evt:RedBoxCastEvent):Void
    {
        var liveCast:LiveCast = evt.params.liveCast
    
        // Subscribe live cast
        var stream:NetStream = avCastMan.subscribeLiveCast(liveCast.id)
    
        // Display a/v stream on stage
        ...
    }

unpublishLiveCast

function unpublishLiveCast (
) : Void

Stop broadcasting the current user's live stream.
Calling this method causes the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastUnpublished event to be fired on the other users clients.
Events broadcasted to listeners:
  • RedBoxCastEvent with type: onLiveCastUnpublished
    • Dispatched when a user in the current room stops his own live stream.
Example:
  • The following example shows how to unpublish a live cast.
    avCastMan.unpublishLiveCast()

unsubscribeAllLiveCasts

function unsubscribeAllLiveCasts (
) : Void

Unsubscribe all currently subscribed live casts.

unsubscribeLiveCast

function unsubscribeLiveCast (
castId:String) : Void

Unsubscribe a live cast to stop receiving its audio/video stream.
NOTE: when a user stops his own stream or leaves the current room / disconnects from SmartFoxServer while his stream is in progress, the AVCastManager class automatically unsubscribes the live cast before dispatching the com.smartfoxserver.redbox.events.RedBoxCastEvent.onLiveCastUnpublished event.
Parameters:
castId:
(String) the id of the com.smartfoxserver.redbox.data.LiveCast object to be unsubscribed.
Example:
  • The following example shows how to unsubscribe a live cast.
    avCastMan.unsubscribeLiveCast(liveCast.id)